import router from '@/router';

const tagsview ={
    state: {
        views: [{title: '系统首页', name: 'Dashboard'}],
        currentName: 'Dashboard'
    },
    actions: {
        addViews({commit}: any, data: any) {
            commit('ADD_VIEWS', data)
            commit('SET_CURRENT_NAME', data.name)
        },
        setCurrentName({commit}: any, data: any) {
            commit('SET_CURRENT_NAME', data.name)
        },
        removeViews({commit}: any, data: any) {
            commit('REMOVE_VIEWS', data)
        }
    },
    mutations: {
        // 新增路由
        ADD_VIEWS: (state: any, data: any) => {
            if (state.views.filter((obj: any) => obj.name == data.name).length == 0) {
                state.views.push(data)
            }
        },
        // 设置当前路由高亮
        SET_CURRENT_NAME: (state: any, name: string) => {
            state.currentName = name
        },
        // 删除路由默认跳转到前一个路由
        REMOVE_VIEWS: (state: any, data: any) => {
            for(const [i, v] of state.views.entries()) {
                if (v.name == data.name) {
                    state.views.splice(i, 1)
                    state.currentName = state.views[i - 1].name
                    router.push({name: state.currentName})
                    break
                }
            }
        }
    }
}

export default tagsview
